package send_message;

import clientservice.keyreader;
import clientservice.service;
import plugin.PluginInterface;
import system.MessageOUTPUT;

public class Main implements PluginInterface {

    private static String[][] groups; // [x][y]  x - group number y - parameter(0 - user, 1 - adress, 2 - port);
    private static String message; // message to transfer;
    private boolean stop;

    public boolean hasParameters() {
        return true;
    }

    public String[] ParameterNames() {
        String[] mes = {"message"};
        return mes;
    }

    public String[] GroupNames() {
        String[] names = {"group"};
        return names;

    }

    public String[][] GroupParameterNames() {
        String[][] groupparameters = new String[1][3];
        groupparameters[0][0] = "user";
        groupparameters[0][1] = "adress";
        groupparameters[0][2] = "port";
        return groupparameters;
    }

    public String About() {
        return "Sending message to destination hosts";
    }

    public boolean Crashed() {
        return false;
    }

    public String CrashError() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Start() {
        stop = false;
        Connect();
    }

    public void Stop() {
        stop = true;
    }

    public void InputParameters(String[][] Plugin_String_parameter, String[][][][] Plugin_Group_String_parameter) {
        message = Plugin_String_parameter[0][0];
        groups = new String[Plugin_Group_String_parameter[0].length][3];
        for (int i = 0; i < Plugin_Group_String_parameter[0].length; i++) {
            groups[i][0] = Plugin_Group_String_parameter[0][i][0][0];
            groups[i][1] = Plugin_Group_String_parameter[0][i][1][0];
            groups[i][2] = Plugin_Group_String_parameter[0][i][2][0];
        }
    }

    private void Connect() {
        for (int i = 0; i < groups.length; i++) {
            if (!stop) {
                String user = groups[i][0];
                String address = groups[i][1];
                int port = Integer.parseInt(groups[i][2]);
                service instance = new service();
                if (instance.Connect(address, port)) {

                    //show message: "connected to address:port"
                    String[] mes_param = {address, "" + port};
                    new MessageOUTPUT().PluginOutput("send_message", "m0001", mes_param);

                    if (instance.LogIn(user, new keyreader().read(user))) {
                        //show message: "logged to address:port as user"
                        mes_param = new String[]{address, "" + port, user};
                        new MessageOUTPUT().PluginOutput("send_message", "m0002", mes_param);

//send command to prepare to load plugin
                        instance.SendMessageByte((byte) 0x01);
//sending plugin name;
                        instance.SendMessageUTF("show_message");

                        //send message
                        instance.SendMessageUTF(message);

                        //show message:"message send to address:port as user"
                        mes_param = new String[]{address, "" + port, user, message};
                        new MessageOUTPUT().PluginOutput("send_message", "m0005", mes_param);


                    } else {
                        //show message: "access deny for user on address:port"
                        mes_param = new String[]{address, "" + port, user};
                        new MessageOUTPUT().PluginOutput("send_message", "m0003", mes_param);
                    }
                    instance.CloseConnection();
                } else {
                    //show message: "connection to address:port failed"
                    String[] mes_param = {address, "" + port};
                    new MessageOUTPUT().PluginOutput("send_message", "m0004", mes_param);
                }
            }

        }
    }
}
